package com.ruoyi.web.controller.ora;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.ora.domain.AssetsRecycle;
import com.ruoyi.ora.domain.Notebook;
import com.ruoyi.ora.domain.VoiceFolder;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ora.domain.SosRoster;
import com.ruoyi.ora.service.ISosRosterService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * SOS名单Controller
 * 
 * @author liuwp
 * @date 2025-05-20
 */
@RestController
@RequestMapping("/ora/sos_roster")
public class SosRosterController extends BaseController
{
    @Autowired
    private ISosRosterService sosRosterService;

    /**
     * 查询SOS名单列表
     */
    @PreAuthorize("@ss.hasPermi('ora:sos_roster:list')")
    @GetMapping("/list")
    public TableDataInfo list(SosRoster sosRoster)
    {
        startPage();
        List<SosRoster> list = sosRosterService.selectSosRosterList(sosRoster);
        return getDataTable(list);
    }

    /**
     * 导出SOS名单列表
     */
    @PreAuthorize("@ss.hasPermi('ora:sos_roster:export')")
    @Log(title = "SOS名单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SosRoster sosRoster)
    {
        List<SosRoster> list = sosRosterService.selectSosRosterList(sosRoster);
        ExcelUtil<SosRoster> util = new ExcelUtil<SosRoster>(SosRoster.class);
        util.exportExcel(response, list, "SOS名单数据");
    }

    /**
     * 获取SOS名单详细信息
     */
    @PreAuthorize("@ss.hasPermi('ora:sos_roster:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sosRosterService.selectSosRosterById(id));
    }

    /**
     * 新增SOS名单
     */
    @PreAuthorize("@ss.hasPermi('ora:sos_roster:add')")
    @Log(title = "SOS名单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SosRoster sosRoster)
    {
        return toAjax(sosRosterService.insertSosRoster(sosRoster));
    }

    /**
     * 修改SOS名单
     */
    @PreAuthorize("@ss.hasPermi('ora:sos_roster:edit')")
    @Log(title = "SOS名单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SosRoster sosRoster)
    {
        return toAjax(sosRosterService.updateSosRoster(sosRoster));
    }

    /**
     * 删除SOS名单
     */
    @PreAuthorize("@ss.hasPermi('ora:sos_roster:remove')")
    @Log(title = "SOS名单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        int delCount = 0;
        for (Long id : ids) {
            delCount += sosRosterService.updateSosRoster(Builder.of(SosRoster::new).with(SosRoster::setId, id).with(SosRoster::setDeleteTime, new Date()).build());
        }
        return toAjax(delCount);
//        return toAjax(sosRosterService.deleteSosRosterByIds(ids));
    }


    @PreAuthorize("@ss.hasPermi('ora:sos_roster:list')")
    @GetMapping("/query")
    public AjaxResult query(SosRoster sosRoster)
    {
        sosRoster.setUserId(SecurityUtils.getUserId());
        List<SosRoster> list = sosRosterService.selectSosRosterList(sosRoster);
        return AjaxResult.success(list);
    }

    @PreAuthorize("@ss.hasPermi('ora:sos_roster:add')")
    @Log(title = "SOS名单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addRoster(SosRoster sosRoster)
    {
        sosRoster.setUserId(SecurityUtils.getUserId());
        return toAjax(sosRosterService.insertSosRoster(sosRoster));
    }

    @Log(title = "SOS名单", businessType = BusinessType.UPDATE)
    @PostMapping("/sort")
    public AjaxResult sort(@RequestBody List<SosRoster> sosRosters)
    {
        sosRosters.forEach(sosRoster -> {
            sosRosterService.updateSosRoster(sosRoster);
        });
        return AjaxResult.success();
    }

}
